/**
 * Copyright (c) 2013-Now http://dreamcode123321.com All rights reserved.
 */
package com.jeesite.modules.ab.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonView;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeType;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.jeesite.common.lang.DateUtils;
import com.jeesite.modules.ab.entity.AbTemp;
import com.jeesite.modules.sys.entity.Menu;
import net.sf.json.util.JSONUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.ab.entity.AbData;
import com.jeesite.modules.ab.service.AbDataService;

import java.io.IOException;
import java.text.DecimalFormat;
import java.util.*;

/**
 * 室温数据表Controller
 * @author d＇c
 * @version 2020-07-12
 */
@Controller
@RequestMapping(value = "${adminPath}/ab/abData")
public class AbDataController extends BaseController {

	@Autowired
	private AbDataService abDataService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public AbData get(String dateCode, boolean isNewRecord) {
		return abDataService.get(dateCode, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequestMapping(value = {"list", ""})
	public String list(AbData abData, Model model) {
		model.addAttribute("abData", abData);
		return "modules/ab/abDataList";
	}



	/**
	 * 查询列表数据
	 */
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<AbData> listData(AbData abData, HttpServletRequest request, HttpServletResponse response) {
		abData.setPage(new Page<>(request, response));
		Page<AbData> page = abDataService.findPage(abData);
		return page;
	}

	/**
	 * rili
	 */
	@RequestMapping(value = "rili")
	public String rili(AbTemp abTemp, Model model) {
		model.addAttribute("abTemp", abTemp);
		return "modules/ab/rili";
	}

	/**
	 * dongtai
	 */
	@RequestMapping(value = "dongtai")
	public String dongtai(AbData abData, Model model) {
		model.addAttribute("abData", abData);
		return "modules/ab/dongtai";
	}

	/**
	 * dynamictemp_full
	 */
	@RequestMapping(value = "dongtai_full")
	public String dynamictemp_full(AbData abData, Model model) {
		model.addAttribute("abData", abData);
		return "modules/ab/dongtai_full";
	}

	/**
	 * dynamictemp_full
	 */
	@RequestMapping(value = "listAll")
	@ResponseBody
	public JSONArray listAll(AbData abData) {
		List<AbData> list = abDataService.getAllList(abData);
		JSONArray jsonArray = new JSONArray();
		for (AbData ab : list) {
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("dataCode", ab.getDataCode());
			jsonObject.put("name", ab.getName());
			jsonObject.put("companyCode", ab.getCompanyCode());
			jsonObject.put("companyName", ab.getCompanyName());
			jsonObject.put("areaCode", ab.getAreaCode());
			jsonObject.put("areaName", ab.getAreaName());
			jsonObject.put("floorCode", ab.getFloorCode());
			jsonObject.put("floorName", ab.getFloorName());
			jsonObject.put("houseCode", ab.getHouseCode());
			jsonObject.put("equipNo", ab.getEquipNo());
			jsonObject.put("inValue", ab.getInValue());
			jsonObject.put("outValue", ab.getOutValue());
			jsonObject.put("createTime", ab.getCreateTime());
			jsonObject.put("beginTime", ab.getBeginTime());
			jsonObject.put("endTime", ab.getEndTime());
			jsonArray.add(jsonObject);
		}
		return jsonArray;
	}

	/**
	 * shebei
	 */
	@RequestMapping(value = "shebei")
	public String shebei(AbData abData, Model model) {
		model.addAttribute("abData", abData);
		return "modules/ab/shebei";
	}

	/**
	 * pingjun
	 */
	@RequestMapping(value = "pingjun")
	public String pingjun(AbData abData, Model model) {
		model.addAttribute("abData", abData);
		return "modules/ab/pingjun";
	}

	/**
	 * pingjun
	 */
	@RequestMapping(value = "pingjunData")
	@ResponseBody
	public Page<AbData> pingjunData(AbData abData, HttpServletRequest request, HttpServletResponse response) {
		abData.setPage(new Page<>(request, response));
		logger.info("=" + abData.getAreaCode() +"=-=" + abData.getCompanyCode() + "=-=" + abData.getFloorCode());
		Page<AbData> page = abDataService.findAvgPage(abData);
		return page;
	}

	/**
	 * 查询列表数据
	 */
	@RequestMapping(value = "indexechart")
	@ResponseBody
	public Map<String, String[]> indexechart(AbData abData, HttpServletRequest request, HttpServletResponse response) {
		Date endTime =  DateUtils.getOfDayLast(DateUtils.parseDate(DateUtils.getDate(DateUtils.getDate(), -1, Calendar.DATE)));
		Date beginTime = DateUtils.getOfDayFirst(DateUtils.parseDate(DateUtils.getDate(DateUtils.getDate(), -30, Calendar.DATE)));
		Map<String, String[]> map = new HashMap<>();
		abData.setBeginTime(beginTime);
		abData.setEndTime(endTime);
		List<AbData> list = abDataService.getAvgList(abData);
		String[] x = new String[list.size()];
		String[] v = new String[list.size()];

		DecimalFormat    df   = new DecimalFormat("#0.0");
		for (int i = 0; i < list.size(); i++) {
			x[i] = DateUtils.formatDate(list.get(i).getCreateTime(),"yyyy-MM-dd");
			v[i] = String .format("%.1f", list.get(i).getInValue());
		}
		map.put("x", x);
		map.put("v", v);
		return map;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("ab:abData:view")
	@RequestMapping(value = "form")
	public String form(AbData abData, Model model) {
		model.addAttribute("abData", abData);
		return "modules/ab/abDataForm";
	}

	/**
	 * 保存室温数据表
	 */
	@RequiresPermissions("ab:abData:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated AbData abData) {
		abDataService.save(abData);
		return renderResult(Global.TRUE, text("保存室温数据表成功！"));
	}
	
	/**
	 * 删除室温数据表
	 */
	@RequiresPermissions("ab:abData:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(AbData abData) {
		abDataService.delete(abData);
		return renderResult(Global.TRUE, text("删除室温数据表成功！"));
	}
	
}